<template>
  <div class="order-preparation-footer-box" :style="{ paddingBottom: `16px` }">
    <nut-button
      block
      v-if="requestTime > 0"
      type="primary"
      @click="cancelAnimationTime"
      color="linear-gradient(to right, #5232B7,#7237BC)"
      >预报价({{ requestTime }}s)(点击取消)
    </nut-button>
    <nut-button
      v-else
      block
      type="primary"
      color="linear-gradient(to right, #5232B7,#7237BC)"
      @click="onTipShow"
      >确认出票</nut-button
    >
    <nut-popup
      v-model:visible="twoShow"
      :style="{ padding: '20px 40px', width: '60%', borderRadius: '15px' }"
    >
      <div class="order-preparation-tip">确认信息</div>
      <div class="order-preparation-confirm">
        <div class="order-preparation-content">
          <div class="row1">
            <div class="title">是否调座位：</div>
            <div class="value">
              <!-- checkedSeat.value == 1 -->
              <nut-tag color="#5232B7" v-if="checkedSeat == 1"
                >九宫格调座</nut-tag
              >
              <nut-tag color="#5232B7" v-else>不可调座</nut-tag>
            </div>
          </div>
          <div class="row2">
            <div class="title">原价：</div>
            <div class="value" style="color: #5232b7">
              ￥ {{ changeTwoDecimal(Price) }}
              <!-- checkedSeat.value == 1 -->
            </div>
          </div>
          <div class="row3" v-if="matchChannelFailstate">
            <div class="title">限价：</div>
            <div class="value" style="color: #5232b7">
              ￥ {{ changeTwoDecimal(inputLimitPrice.Price) }}
              <!-- checkedSeat.value == 1 -->
            </div>
          </div>

          <div class="row3" v-if="Text1">
            <div class="title" style="color: #5232b7">tip1：</div>
            <div class="value" style="color: #5232b7; font-size: 13px">
              出票价不高于{{ changeTwoDecimal(maxPrice) }}元,请留意限价！
            </div>
          </div>
          <div class="row3" v-if="Text2">
            <div class="title" style="color: #5232b7">tip2：</div>
            <div class="value" style="color: #5232b7; font-size: 13px">
              选择的一口价渠道价格未知,请留意！
              <!-- checkedSeat.value == 1 -->
            </div>
          </div>
        </div>
        <div class="footer-btn-row">
          <nut-button
            type="default"
            size="small"
            @click="twoShow = false"
            style="flex: 1"
            >取消</nut-button
          >
          <div style="width: 20px"></div>
          <nut-button
            style="flex: 1"
            type="primary"
            size="small"
            color="linear-gradient(to right, #5232B7,#7237BC)"
            @click="purchase"
            >确定</nut-button
          >
        </div>
      </div>
    </nut-popup>
  </div>
</template>
<script setup lang="ts">
import Taro from "@tarojs/taro";
import { storeToRefs } from "pinia";
import { useUserStore } from "../../store";
import { toRefs, ref } from "vue";
import * as orderApi from "../../api/order.ts";
const UserStore = useUserStore();
const { Userinfo } = storeToRefs(UserStore);
const emits = defineEmits(["onCancelTime"]);
const twoShow = ref(false); //二次确认弹框
// 接收父组件传递过来的props
const props = defineProps({
  radioValList: {
    type: Array,
    required: true,
  },
  selectedSeat: {
    type: Array,
    default: [],
  },
  arealist: {
    type: Array,
    default: [],
  },
  rateList: {
    type: Array,
    default: [],
  },
  currentShowId: {
    type: String,
    required: true,
  },
  matchChannelFailstate: {
    type: Boolean,
    required: true,
  },
  inputLimitPrice: {
    type: Object,
    required: true,
  },
  checkedSeat: {
    type: String,
    required: true,
  },
  requestTime: {
    type: Number,
    required: true,
  },
  Price: {
    type: Number,
    required: true,
  }, //原价
});
const {
  radioValList,
  selectedSeat,
  currentShowId,
  matchChannelFailstate,
  inputLimitPrice,
  checkedSeat,
  arealist,
  rateList,
  Price,
  requestTime,
} = toRefs(props);

const Text1 = ref(false);
const Text2 = ref(false);
const maxPrice = ref("");

//提示价格
const onTipShow = async () => {
  showNowText();
  setTimeout(() => {
    twoShow.value = true;
  }, 300);
};
// 展示当前的文字  当前的tip1 tip2 的文字 提示
const showNowText = () => {
  try {
    if (radioValList.value.length == 1) {
      if (radioValList.value[0]?.isSupport == 1) {
        if (radioValList.value[0]?.netPrice > 0) {
          maxPrice.value = (radioValList.value[0]?.netPrice / 100).toFixed(2);
        } else {
          maxPrice.value = changeTwoDecimal(Price.value * 0.95);
        }
        Text1.value = true;
      } else if (radioValList.value[0]?.isSupport == 0) {
        if (radioValList.value[0]?.netPrice > 0) {
          Text1.value = true;
          maxPrice.value = (radioValList.value[0]?.netPrice / 100).toFixed(2);
        } else {
          Text1.value = false;
          Text2.value = true;
        }
      }
    } else if (radioValList.value.length == 2) {
      let newList1 = radioValList.value.filter(
        (item: any) => item.isSupport == 0
      );
      let newList2 = radioValList.value.filter(
        (item: any) => item.isSupport == 1
      );
      console.log("newList1", newList1);
      console.log("newList2", newList2);

      if (newList1.length == 1) {
        console.log(newList1.length, "newList1.length");

        if (newList1[0]?.netPrice > 0 && newList2[0]?.netPrice > 0) {
          if (newList1[0]?.netPrice > newList2[0]?.netPrice) {
            maxPrice.value = (newList1[0]?.netPrice / 100).toFixed(2);
            Text1.value = true;
            Text2.value = false;
          } else {
            maxPrice.value = (newList2[0]?.netPrice / 100).toFixed(2);
            Text1.value = true;
            Text2.value = false;
          }
        } else if (newList1[0]?.netPrice > 0 && newList2[0]?.netPrice == 0) {
          maxPrice.value = (newList1[0]?.netPrice / 100).toFixed(2);
          Text1.value = true;
        } else if (newList1[0]?.netPrice == 0 && newList2[0]?.netPrice > 0) {
          maxPrice.value = (newList2[0]?.netPrice / 100).toFixed(2);
          Text1.value = true;
          Text2.value = true;
        } else if (newList1[0]?.netPrice == 0 && newList2[0]?.netPrice == 0) {
          maxPrice.value = changeTwoDecimal(Price.value * 0.95);
          Text1.value = true;
          Text2.value = true;
        }
      } else if (newList1.length == 2) {
        console.log(newList1.length, "newList1.length");

        if (newList1[0]?.netPrice > 0 && newList1[1]?.netPrice > 0) {
          if (newList1[0]?.netPrice > newList1[1]?.netPrice) {
            maxPrice.value = (newList1[0]?.netPrice / 100).toFixed(2);
            Text1.value = true;
            Text2.value = false;
          } else {
            maxPrice.value = (newList2[1]?.netPrice / 100).toFixed(2);
            Text1.value = true;
            Text2.value = false;
          }
        } else if (newList1[0]?.netPrice > 0 && newList1[1]?.netPrice == 0) {
          maxPrice.value = (newList1[0]?.netPrice / 100).toFixed(2);
          Text1.value = true;
          Text2.value = true;
        } else if (newList1[0]?.netPrice == 0 && newList1[1]?.netPrice > 0) {
          maxPrice.value = (newList1[1]?.netPrice / 100).toFixed(2);
          Text1.value = true;
          Text2.value = true;
        }else if(newList1[0]?.netPrice == 0 && newList1[1]?.netPrice == 0){
          Text1.value = false;
          Text2.value = true;
        }
      } else if (newList1.length == 0) {
        console.log(newList1.length, "newList1.length");
        Text2.value = false;

        if (newList2[0]?.netPrice > 0 && newList2[1]?.netPrice > 0) {
          console.log(1);

          if (newList2[0]?.netPrice > newList2[1]?.netPrice) {
            maxPrice.value = (newList2[0]?.netPrice / 100).toFixed(2);
            Text1.value = true;
            Text2.value = false;
          } else {
            maxPrice.value = (newList2[1]?.netPrice / 100).toFixed(2);
            Text1.value = true;
            Text2.value = false;
          }
        } else if (newList2[0]?.netPrice > 0 && newList2[1]?.netPrice == 0) {
          console.log(2);

          maxPrice.value = (newList2[0]?.netPrice / 100).toFixed(2);
          Text1.value = true;
          Text2.value = false;
        } else if (newList2[0]?.netPrice == 0 && newList2[1]?.netPrice > 0) {
          console.log(3);

          maxPrice.value = (newList2[1]?.netPrice / 100).toFixed(2);
          Text1.value = true;
          Text2.value = false;
        } else if (newList2[0]?.netPrice == 0 && newList2[1]?.netPrice == 0) {
          console.log(4);

          maxPrice.value = changeTwoDecimal(Price.value * 0.95);
          Text1.value = true;
        }
      }
    }
  } catch (error) {}
};

// 锁座下单
const purchase = async () => {
  let limit_Price = Number(changeTwoDecimal(inputLimitPrice.value.Price * 100));
  if (limit_Price == 0) {
    Taro.showToast({
      title: "限价不能等于0",
      icon: "none",
    });
    return;
  }
  Taro.showLoading({
    title: "加载中",
  });
  if (radioValList.value.length === 0) {
    Taro.showToast({
      title: "主渠道必选~",
      icon: "none",
    });

    return;
  }
  //选择当前的 座位数据
  const seatData = selectedSeat.value.map((item: any) => {
    return {
      col: item.col,
      row: item.row,
      seatName: item.seatNo,
    };
  });
  const resultData = {
    // userName: 13931933190,
    userId: Userinfo.value.id,
    showId: currentShowId.value,
    seats: seatData,
    channel: 3,
    orderType: 1,
    changeSeat: checkedSeat.value == 1 ? 1 : 0,
    inputLimitPrice: changeTwoDecimal(inputLimitPrice.value.Price)
      ? Number(changeTwoDecimal(inputLimitPrice.value.Price * 100))
      : 0,
    matchChannel: radioValList.value[0]?.id ? radioValList.value[0]?.id : 0,
    // matchChannelFailState: matchChannelFailstate.value ? 1 : 0,
    slaveMatchChannel: radioValList.value[1]?.id
      ? radioValList.value[1]?.id
      : 0,
  };
  try {
    const res: any = await orderApi.createOrder(resultData);
    if (res.state == 200) {
      Taro.redirectTo({
        url: `/pages/order-detail/index?orderId=${res.data.orderNumber}`,
      });
      Taro.hideLoading();
    } else if (res.state == 202 || res.state == 203 || res.state == 201) {
      Taro.hideLoading();
      Taro.showToast({
        title: res.message,
        icon: "none",
      });
    }
  } catch (err) {
    Taro.hideLoading();
    console.log(err, "err");
    Taro.showToast({
      title: "创建失败，请稍后尝试",
      icon: "none",
    });
    return err;
  }
};
// 取消当前的倒计时
const cancelAnimationTime = () => {
  emits("onCancelTime");
};
// 改变当前的 数字两位小数
const changeTwoDecimal = (x) => {
  let resNum = parseFloat(x);
  // isNaN()，判断一个变量是否为非数字的类型，非数字返回true，数字则返回false
  // console.log('你看=', isNaN(resNum))
  if (isNaN(resNum)) {
    return false;
  }
  resNum = Math.round(resNum * 100) / 100;
  return resNum;
};
</script>
<style lang="less">
.order-preparation-footer-box {
  display: flex;
  flex-direction: column;
  margin-top: 30px;
  // position: fixed;
  // bottom: 0;
  // left: 0;
  // right: 0;
  padding: 0 10px;
  .order-preparation-tip {
    color: #000;
    font-weight: 700;
    text-align: center;
    width: 100%;
    font-size: 28px;
  }
  .order-preparation-confirm {
    .order-preparation-content {
      // height: 300px;
      padding: 30px 0;
      .row1,
      .row2,
      .row3 {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      .row3 {
        margin-top: 10px;
      }
      .row2 {
        margin-top: 10px;
      }
    }

    .footer-btn-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 30px;
    }
  }
}
</style>
